<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CryptoHash - MD5加密/解密工具</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3a0ca3;
            --accent: #7209b7;
            --light: #f8f9fa;
            --dark: #212529;
            --success: #4cc9f0;
            --warning: #f72585;
            --gray: #6c757d;
            --border-radius: 12px;
            --box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            color: var(--dark);
        }

        .container {
            width: 100%;
            max-width: 800px;
            background: white;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            overflow: hidden;
            margin: 20px 0;
        }

        header {
            background: linear-gradient(to right, var(--primary), var(--secondary));
            color: white;
            padding: 25px;
            text-align: center;
        }

        header h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
        }

        header p {
            opacity: 0.9;
            font-size: 1.1rem;
        }

        .tabs {
            display: flex;
            background: #e9ecef;
            border-bottom: 1px solid #dee2e6;
        }

        .tab {
            padding: 15px 25px;
            cursor: pointer;
            font-weight: 600;
            text-align: center;
            flex: 1;
            transition: var(--transition);
            border-bottom: 3px solid transparent;
        }

        .tab.active {
            border-bottom: 3px solid var(--primary);
            color: var(--primary);
            background: white;
        }

        .tab-content {
            display: none;
            padding: 30px;
        }

        .tab-content.active {
            display: block;
            animation: fadeIn 0.5s ease;
        }

        .input-group {
            margin-bottom: 25px;
        }

        .input-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: var(--dark);
        }

        .input-wrapper {
            position: relative;
            display: flex;
            align-items: center;
        }

        .input-wrapper i {
            position: absolute;
            left: 15px;
            color: var(--gray);
        }

        input {
            width: 100%;
            padding: 15px 15px 15px 45px;
            border: 2px solid #e9ecef;
            border-radius: var(--border-radius);
            font-size: 16px;
            transition: var(--transition);
        }

        input:focus {
            border-color: var(--primary);
            outline: none;
            box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.2);
        }

        button {
            background: linear-gradient(to right, var(--primary), var(--secondary));
            color: white;
            border: none;
            padding: 15px 25px;
            border-radius: var(--border-radius);
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            width: 100%;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(67, 97, 238, 0.4);
        }

        button:disabled {
            background: var(--gray);
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .result-box {
            margin-top: 25px;
            padding: 20px;
            border-radius: var(--border-radius);
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            display: none;
        }

        .result-box.visible {
            display: block;
            animation: fadeIn 0.5s ease;
        }

        .result-box h3 {
            margin-bottom: 15px;
            color: var(--dark);
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .result-content {
            word-break: break-all;
            padding: 15px;
            background: white;
            border-radius: 8px;
            border: 1px solid #dee2e6;
            font-family: monospace;
            font-size: 16px;
            color: var(--dark);
        }

        .copy-btn {
            background: var(--gray);
            padding: 8px 15px;
            font-size: 14px;
            margin-top: 10px;
            width: auto;
        }

        .copy-btn:hover {
            background: #5a6268;
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        footer {
            text-align: center;
            padding: 20px;
            color: var(--gray);
            font-size: 14px;
        }

        footer a {
            color: var(--primary);
            text-decoration: none;
        }

        footer a:hover {
            text-decoration: underline;
        }

        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px 25px;
            background: var(--success);
            color: white;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            transform: translateX(100%);
            opacity: 0;
            transition: var(--transition);
            z-index: 1000;
        }

        .notification.show {
            transform: translateX(0);
            opacity: 1;
        }

        .notification.error {
            background: var(--warning);
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        @media (max-width: 600px) {
            header h1 {
                font-size: 1.8rem;
            }

            .tab {
                padding: 12px 15px;
                font-size: 14px;
            }

            .tab-content {
                padding: 20px;
            }

            input, button {
                padding: 12px;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1><i class="fas fa-lock"></i> CryptoHash</h1>
        <p>安全可靠的MD5加密与解密工具</p>
    </header>

    <div class="tabs">
        <div class="tab active" data-tab="encrypt">
            <i class="fas fa-lock"></i> 加密
        </div>
        <div class="tab" data-tab="decrypt">
            <i class="fas fa-unlock"></i> 解密
        </div>
    </div>

    <div class="tab-content active" id="encrypt-tab">
        <div class="input-group">
            <label for="text"><i class="fas fa-font"></i> 输入明文</label>
            <div class="input-wrapper">
                <i class="fas fa-pencil-alt"></i>
                <input type="text" id="text" placeholder="输入要加密的文本...">
            </div>
        </div>

        <button onclick="encrypt()" id="encryptBtn">
            <i class="fas fa-lock"></i> 加密文本
        </button>

        <div class="result-box" id="encryptResult">
            <h3><i class="fas fa-hashtag"></i> MD5 加密结果</h3>
            <div class="result-content" id="md5"></div>
            <button class="copy-btn" onclick="copyResult('md5')">
                <i class="fas fa-copy"></i> 复制结果
            </button>
        </div>
    </div>

    <div class="tab-content" id="decrypt-tab">
        <div class="input-group">
            <label for="md5Input"><i class="fas fa-hashtag"></i> 输入MD5哈希值</label>
            <div class="input-wrapper">
                <i class="fas fa-code"></i>
                <input type="text" id="md5Input" placeholder="输入要解密的MD5值...">
            </div>
        </div>

        <button onclick="decode()" id="decryptBtn">
            <i class="fas fa-unlock"></i> 尝试解密
        </button>

        <div class="result-box" id="decryptResult">
            <h3><i class="fas fa-font"></i> 解密结果</h3>
            <div class="result-content" id="decode"></div>
            <button class="copy-btn" onclick="copyResult('decode')">
                <i class="fas fa-copy"></i> 复制结果
            </button>
        </div>
    </div>

    <footer>
        <p>注意：MD5是一种单向哈希函数，无法真正"解密"。此工具通过预计算常见字符串的哈希值来实现反向查找。</p>
        <p>© 2023 CryptoHash | 仅供学习使用</p>
    </footer>
</div>

<div class="notification" id="notification">内容已复制到剪贴板！</div>

<script>
    // 切换标签页
    document.querySelectorAll('.tab').forEach(tab => {
        tab.addEventListener('click', () => {
            document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
            document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));

            tab.classList.add('active');
            document.getElementById(`${tab.dataset.tab}-tab`).classList.add('active');
        });
    });

    // 加密函数
    async function encrypt() {
        const text = document.getElementById("text").value;
        const encryptBtn = document.getElementById('encryptBtn');

        if (!text) {
            showNotification('请输入要加密的文本', true);
            return;
        }

        // 显示加载状态
        encryptBtn.innerHTML = '<div class="loading"></div> 加密中...';
        encryptBtn.disabled = true;

        try {
            const form = new URLSearchParams();
            form.append("text", text);
            const res = await fetch("/api/md5", { method: "POST", body: form });
            const data = await res.json();

            document.getElementById("md5").innerText = data.md5;
            document.getElementById("md5Input").value = data.md5;
            document.getElementById("encryptResult").classList.add("visible");

            showNotification('加密成功！');
        } catch (error) {
            console.error("加密错误:", error);
            showNotification('加密请求失败，请重试', true);
        } finally {
            // 恢复按钮状态
            encryptBtn.innerHTML = '<i class="fas fa-lock"></i> 加密文本';
            encryptBtn.disabled = false;
        }
    }

    // 解密函数
    async function decode() {
        const md5 = document.getElementById("md5Input").value;
        const decryptBtn = document.getElementById('decryptBtn');

        if (!md5) {
            showNotification('请输入要解密的MD5哈希值', true);
            return;
        }

        // 验证MD5格式（32位十六进制）
        if (!/^[a-f0-9]{32}$/i.test(md5)) {
            showNotification('请输入有效的MD5哈希值（32位十六进制）', true);
            return;
        }

        // 显示加载状态
        decryptBtn.innerHTML = '<div class="loading"></div> 解密中...';
        decryptBtn.disabled = true;

        try {
            const form = new URLSearchParams();
            form.append("md5", md5);
            const res = await fetch("/api/decode", { method: "POST", body: form });

            if(res.status === 200){
                const data = await res.json();
                document.getElementById("decode").innerText = data.text;
                showNotification('解密成功！');
            } else {
                document.getElementById("decode").innerText = "未找到对应文本";
                showNotification('未找到匹配的原始文本', true);
            }

            document.getElementById("decryptResult").classList.add("visible");
        } catch (error) {
            console.error("解密错误:", error);
            showNotification('解密请求失败，请重试', true);
        } finally {
            // 恢复按钮状态
            decryptBtn.innerHTML = '<i class="fas fa-unlock"></i> 尝试解密';
            decryptBtn.disabled = false;
        }
    }

    // 复制结果函数
    function copyResult(elementId) {
        const text = document.getElementById(elementId).textContent;
        navigator.clipboard.writeText(text).then(() => {
            showNotification('内容已复制到剪贴板！');
        });
    }

    // 显示通知
    function showNotification(message, isError = false) {
        const notification = document.getElementById('notification');
        notification.textContent = message;
        notification.className = 'notification' + (isError ? ' error' : '');
        notification.classList.add('show');

        setTimeout(() => {
            notification.classList.remove('show');
        }, 3000);
    }

    // 按Enter键触发操作
    document.getElementById('text').addEventListener('keypress', (e) => {
        if (e.key === 'Enter') encrypt();
    });

    document.getElementById('md5Input').addEventListener('keypress', (e) => {
        if (e.key === 'Enter') decode();
    });
</script>
</body>
</html>